<?php


namespace App\Shuixing\Controllers;


use App\Admin\Repositories\WxMenu;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\PostUtils;
use App\Wen\Utils\Utils;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxLike;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsImg;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxPostsVideo;
use App\Models\Posts\WxTag;
use App\Models\WxCommentLike;
use App\Models\User\WxUser;

class SingleController
{
    public function index($id)
    {
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        $__MINI_GLOBAL_IS_ADMIN_SUPER__ = false;

        $data = WxPost::where('id', $id)
            ->first();
        if(_empty_($data)){
            return '<h4 style="text-align: center;margin-top: 100px;">404 无记录</h4>';
        }
        $data->user = WxUser::where('id', $data->user_id)
            ->first(FieldUtils::userInfoColums());//发帖人
        $tagsIds = WxPostsTag::where('posts_id', $id)->pluck('tags_id');//话题ID列表
        $data->tags = WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//话题列表
        $data->like_count = WxLike::where('posts_id', $id)->count(); // 笔记喜欢数量
        $data->comment_count = WxComment::where([['posts_id', '=', $id], ['comment_state', '=', 1]])->count(); // 评论数量
//        $data->image = WxPostsImg::where('posts_id', $id)->get(); // 笔记图片
        $data->image = WxPostsImg::where('posts_id', $id)->get(['img_url', 'posts_id'])->map( function ($postImg){
            $postImg->img_url = Utils::imgWithStyle($postImg->img_url, 0);
            return $postImg;
        });//图片

        // 解析shopcard
        if($data->posts_content){
            $data->posts_content = preg_replace_callback('$<shopcard id="(\d+)" img="([^\s]+)" title="(.*?)" price="([\d\.]+)"></shopcard>$', function ($r){
                $id = $r[1];
                $img = $r[2];
                $title = $r[3];
                $price = $r[4];
                $shop_detail_code =Utils::getMiniCode('pagesA/shop/goods-details/goods-details', 'id='.$id);
                return '<div class="shopcard"><div class="left"><img src="'.$img.'"></div><div class="right"><div class="title">'.$title.'</div><div class="price">'.$price.'元</div><div class="go-shop-button go-shop-button-'.$id.'">去购买<img class="shop-detail-code shop-detail-code-'.$id.'" style="display: none" src="'.$shop_detail_code.'"></div></div></div><style>.go-shop-button-'.$id.':hover .shop-detail-code-'.$id.'{display: block!important;}</style>';
            }, $data->posts_content);
        }

        $data->video = WxPostsVideo::where('posts_id', $id)->first(['video_thumb_url', 'video_url']);
        $data->format_time = format_datetime($data->created_at); // 发帖时间
        $data->posts_content_raw = preg_replace("/<(img|video).*?src[^\'\"]+[\'\"]([^\"\']+)[^>]+>/is", '',$data->posts_content);
        $data->posts_content_raw = preg_replace('$(\s|&nbsp;)+$', '', strip_tags(html_entity_decode($data->posts_content_raw)));

        //评论
        $comment = WxComment::where('comment_state', 1)->where('posts_id', $id)
            ->where('comment_id', null)
            ->get();
        $comment->append(['format_time']);
        $comment->map(function ($item) {
            $imgList = array();
            array_push($imgList, ['img_url' => $item->comment_img_url]);
            $item->like_count = WxCommentLike::where('comment_id', $item->id)->count();
            $item->imgList = $imgList;
            $item->comment_content = preg_replace_callback('$<mini-emoji alt="[^"]*?" emoji="[^"]*?"[^>]*?></mini-emoji>$', function($v){
                return str_replace(['mini-emoji', 'emoji='], ['img', 'src='], $v[0]);
            }, $item->comment_content);
            $childData = WxComment::where('comment_id', $item->id)->get(['id', 'user_name', 'comment_content', 'user_id', 'posts_user_id', 'user_avatar', 'comment_img_url', 'created_at', 'comment_agent_name', 'comment_agent_id']);
            $childData->map(function ($item) {
                $imgList = array();
                array_push($imgList, ['img_url' => $item->comment_img_url]);
                $item->like_count = WxCommentLike::where('comment_id', $item->id)->count();
                $item->imgList = $imgList;
                $item->comment_content = preg_replace_callback('$<mini-emoji alt="[^"]*?" emoji="[^"]*?"[^>]*?></mini-emoji>$', function($v){
                    return str_replace(['mini-emoji', 'emoji='], ['img', 'src='], $v[0]);
                }, $item->comment_content);
            });
            $childData->append(['format_time']);

            $item->child = $childData;
        });

        $link = url()->current();

        $seo_title = PostUtils::get_the_title($data->posts_content_raw, $data->id);
        $seo_description = str_replace($seo_title, '', $data->posts_content_raw);
        if(mb_strlen($seo_description) > 50){
            $seo_description = mb_substr($seo_description,0 , 50) .'...';
        }

        return view('shuixing/single', ['post_id'=>$id, 'data' => $data, 'comment' => $comment, 'link' => $link, 'seo_title'=>$seo_title, 'seo_description'=>$seo_description]);
    }
}
